package com.itheima.project.web;

import com.itheima.project.pojo.Customer;
import com.itheima.project.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description：
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;


    /**
     * @Description 查询用户列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public ModelAndView list(){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("list", customerService.findCustomerAll());
        return new ModelAndView("customer/list").addAllObjects(map);
    }

    /**
     * @Description 去添加用户页面
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd(){
        return new ModelAndView("customer/add");
    }

    /**
     * @Description 添加用户
     */
    @RequestMapping(value = "/doAdd",method = RequestMethod.POST)
    public String doAdd(Customer customer){
        customerService.saveCustomer(customer);
        return "redirect:/customer/list";
    }

    /**
     * @Description 删除用户
     */
    @RequestMapping("/doDel")
    public String doDel(Integer customerId){
        customerService.deleteCustomerById(customerId);
        return "redirect:/customer/list";
    }

    /**
     * @Description 去修改用户页面
     */
    @RequestMapping(value = "/toUpdate")
    public ModelAndView toUpdate(Integer customerId){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("customer", customerService.findCustomerById(customerId));
        return new ModelAndView("customer/update").addAllObjects(map);
    }

    /**
     * @Description 修改用户
     */
    @RequestMapping(value = "/doUpdate",method = RequestMethod.POST)
    public String doUpdate(Customer customer){
        customerService.updateCustomerById(customer);
        return "redirect:/customer/list";
    }



}
